package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityPlanToDo;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityPlanToDoDao;
import cc.comac.mom.entity.QualityPlanToDoEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.MomConstants;
import cc.comac.mom.util.ResultBean;

@Component
public class QualityPlanToDoDataHandler extends ServiceImpl<QualityPlanToDoDao, QualityPlanToDoEntity> {

	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private QualityPlanToDoDao qualityPlanToDoDao;
	
	/**
	 * 插入计划代办信息
	 * @param obj
	 * @return
	 */
	public ResultBean insert(QualityPlanToDo obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		QualityPlanToDoEntity innerData = obj.getInnerData();
		if(innerData.getPartNo() == null || "".equals(innerData.getPartNo())){
			resultBean.setErrorMessge("件号/图号不能为空");
			return resultBean;
		}
		if(innerData.getToDoType() == 0) {
			resultBean.setErrorMessge("计划待办类型不能为空");
			return resultBean;
		}
		if(innerData.getStatus() <0) {
			resultBean.setErrorMessge("计划待办状态不能为空");
			return resultBean;
		}
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = qualityPlanToDoDao.insert(innerData);
		if(insert <1) {
			resultBean.setErrorMessge("计划待办插入失败");
			return resultBean;
		}
		return resultBean;
		
	}
	
	/**
	 * 批量插入计划代办信息
	 * @param obj
	 * @return
	 */
	public ResultBean insertBatch(List<QualityPlanToDo> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		
		ArrayList<QualityPlanToDoEntity> listQualityPlanToDoEntity = new ArrayList<QualityPlanToDoEntity>();
		for(QualityPlanToDo item :list) {
			QualityPlanToDoEntity innerData = item.getInnerData();
			if(innerData.getPartNo() == null || "".equals(innerData.getPartNo())){
				resultBean.setErrorMessge("件号/图号不能为空");
				return resultBean;
			}
			if(innerData.getToDoType() == 0) {
				resultBean.setErrorMessge("计划待办类型不能为空");
				return resultBean;
			}
			if(innerData.getStatus() <0) {
				resultBean.setErrorMessge("计划待办状态不能为空");
				return resultBean;
			}
			listQualityPlanToDoEntity.add(innerData);
		}

		for(QualityPlanToDoEntity item :listQualityPlanToDoEntity) {
			baseEntityDataUtil.setBaseEntityData(item);
		}
		boolean saveBatch = this.saveBatch(listQualityPlanToDoEntity);
		if(!saveBatch) {
			resultBean.setErrorMessge("计划待办插入失败");
			return resultBean;
		}
		return resultBean;
		
	}
	
	
	/**
	 * 获取未处理的计划代办信息通过零件号和待办类型
	 * @param partNo 零件号
	 * @param materialNumber 物料号
	 * @param toDoType 待办类型 1.驳回，2.母本异常 3.母本升版
	 * @return
	 */

	public List<QualityPlanToDo> getUnTreatedByPartNoAndToDoStatus(String partNo,List<Integer> toDoType,String materialNumber){
		
		QueryWrapper<QualityPlanToDoEntity> queryWrapper = new QueryWrapper<QualityPlanToDoEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("part_no", partNo);
		if (StringUtils.isNotEmpty(materialNumber)) {
			queryWrapper.eq("material_number", materialNumber);
		}
		queryWrapper.in("to_do_type", toDoType);
		queryWrapper.eq("status",MomConstants.QUALITY_PLAN_TO_DO_STATUS_UNTREATED);//未处理的待办信息
		List<QualityPlanToDoEntity> selectByMap = qualityPlanToDoDao.selectList(queryWrapper);
		if(selectByMap != null && selectByMap.size()>0) {
			ArrayList<QualityPlanToDo> list = new ArrayList<QualityPlanToDo>();
			for(QualityPlanToDoEntity item :selectByMap) {
				QualityPlanToDo qualityPlanToDo = new QualityPlanToDo();
				qualityPlanToDo.setInnerData(item);
				list.add(qualityPlanToDo);
			}
			return list;
		}
		return null;
	}
	
	/**
	 * 计划代办确认
	 * @param ids
	 * @return
	 */
	public ResultBean planConfirmBatch(List<Long> ids) {
		ResultBean resultBean = new ResultBean(true);
		if(ids == null || ids.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		List<QualityPlanToDoEntity> selectBatchIds = qualityPlanToDoDao.selectBatchIds(ids);
		if(selectBatchIds == null || selectBatchIds.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		for(QualityPlanToDoEntity item:selectBatchIds) {
			item.setStatus(MomConstants.QUALITY_PLAN_TO_DO_STATUS_TREATED);
			baseEntityDataUtil.updateBaseEntityData(item);
		}
		boolean batchById = this.updateBatchById(selectBatchIds);
		if(!batchById) {
			resultBean.setErrorMessge("计划待办确认失败");
			return resultBean;
		}
		return resultBean;
	}
	
	//查詢
	public LimitPageBean<QualityPlanToDoEntity> selectByCondition(QualityPlanToDo obj) {
    	QueryWrapper<QualityPlanToDoEntity> queryWrapper = new QueryWrapper<QualityPlanToDoEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj.getPageSize() == 0) {
    		obj.setPageSize(10);
    	}
		if(obj != null && obj.getInnerData() != null) {	
    		if(obj.getInnerData().getPartNo() != null && !"".equals(obj.getInnerData().getPartNo())) {
    			queryWrapper.like("part_no",obj.getInnerData().getPartNo());
    		}
    		if(obj.getInnerData().getMaterialNumber() != null && !"".equals(obj.getInnerData().getMaterialNumber())) {
    			queryWrapper.like("material_number",obj.getInnerData().getMaterialNumber());
    		}
    		if(obj.getInnerData().getPartName()!= null && !"".equals(obj.getInnerData().getPartName())) {
    			queryWrapper.like("part_name",obj.getInnerData().getPartName());
    		}
    		if(obj.getInnerData().getStatus() != -1) {
    			queryWrapper.eq("status",obj.getInnerData().getStatus());
    		}
    		if(obj.getInnerData().getToDoType() != 0) {
    			queryWrapper.eq("to_do_type",obj.getInnerData().getToDoType());
    		}

		}
		Page<QualityPlanToDoEntity> page = new Page<>(obj.getPageNum(),obj.getPageSize(),true);
		IPage<QualityPlanToDoEntity> iPage = qualityPlanToDoDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<QualityPlanToDoEntity> limitPageBean = (LimitPageBean<QualityPlanToDoEntity>) LimitPageBean.of(iPage);
		return limitPageBean;
		
	}

	// 逻辑删除对应的代办任务
	public void logicDeletePlanTodoList(List<QualityPlanToDo> list) {
		if (list.size() > 0) {
			List<QualityPlanToDoEntity> collect = list.stream().map(QualityPlanToDo::getInnerData).collect(Collectors.toList());
			collect.forEach(item -> {baseEntityDataUtil.deleteBaseEntityData(item);item.setStatus(MomConstants.QUALITY_PLAN_TO_DO_STATUS_TREATED);});
			// 逻辑删除代办任务
			this.updateBatchById(collect, collect.size());
		}
	}

	
	public  QualityPlanToDoEntity selectOneByQuery(QueryWrapper<QualityPlanToDoEntity> query) {
		QualityPlanToDoEntity selectOne = qualityPlanToDoDao.selectOne(query);
		return selectOne;
	}
	
}
